package ui;

public class StopwatchThread implements Runnable {

	public static final int STOPWATCH_DEFAULT_INIT = 0;
	int time;
	boolean mapClickDetected;
	
	@Override
	public synchronized void run() {
		
		time = STOPWATCH_DEFAULT_INIT;
		mapClickDetected = false;
		
		try {
			// 1 second loops
			while( true ) { //there's a break inside
				
				//update stopper label
				MainWindow.display.asyncExec(new Runnable() {
					public void run() {
						MainWindow.lblCountdown.setText(Integer.toString(time));
					}
				});
				
				//pinIt and break when user clicked the map
				MainWindow.display.asyncExec(new Runnable() {
					public void run() {
						mapClickDetected = (boolean) MainWindow.browser.evaluate("return clickDetected");
					}
				});
				if (mapClickDetected) {
					MainWindow.display.asyncExec(new Runnable() {
						public void run() {
							MainWindow.PinIt();
						}
					});
					
					break;
				}
				
				//sleep for a second and repeat
				Thread.sleep(1000);
				time++;
			}
		} catch (InterruptedException e) {
			//add code for interruption here if needed
		}
	}
	
}